package com.encryption.logic.impl;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.encryption.dao.FilePermissionAssoDAO;
import com.encryption.logic.FilePermissionService;
import com.encryption.model.FilePermissionAsso;
/**
 * 文件权限服务接口实现类
 * @author Administrator
 *
 */
public class FilePermissionServiceImpl implements FilePermissionService {

	private static final Logger log = LoggerFactory.getLogger(FilePermissionServiceImpl.class);

	private static FilePermissionServiceImpl filePermissionServiceImpl = null;
	
	
	public static  FilePermissionServiceImpl getFilePermissionServiceImpl(){
		if(null == filePermissionServiceImpl) {
			filePermissionServiceImpl = new FilePermissionServiceImpl();
		} 
		return filePermissionServiceImpl;
	}
	
	/**
	 * 根据文件权限ID获取文件权限对象
	 */
	public FilePermissionAsso getById(String id) {
		return FilePermissionAssoDAO.getFilePermissionAssoDAO().findById(Integer.parseInt(id));
	}
	/**
	 * 添加文件权限
	 */
	public boolean add(FilePermissionAsso permission) {
		try {
			FilePermissionAssoDAO.getFilePermissionAssoDAO().save(permission);
		} catch (Exception e) {
			log.error("add FilePermissionAsso is error ",e);
			return false;
		}
		return true;

	}
	/**
	 * 更新文件权限 
	 */
	public boolean update(FilePermissionAsso permission) {
		try {
			FilePermissionAssoDAO.getFilePermissionAssoDAO().merge(permission);
		} catch (Exception e) {
			log.error("update FilePermissionAsso is error ",e);
			return false;
		}
		return true;
	}

	/**
	 * 删除权限
	 * @param id
	 * @return
	 */
	public boolean deleteActionPermission(String id){
		try {
			FilePermissionAssoDAO.getFilePermissionAssoDAO().delete(FilePermissionAssoDAO.getFilePermissionAssoDAO().findById(Integer.parseInt(id)));
		} catch (Exception e) {
			log.error("delete FilePermissionAsso is error ",e);
			return false;
		}
		return true;
	}

	public List<FilePermissionAsso> getByFileId(int fileId) {
		return FilePermissionAssoDAO.getFilePermissionAssoDAO().findByProperty("file", fileId);
	}
 
}
